package elm.view.impl;

import elm.dao.BusinessDao;
import elm.dao.impl.BusinessDaoImpl;
import elm.pojo.Business;
import elm.view.BusinessView;

import java.util.List;
import java.util.Scanner;

public class BusinessViewImpl implements BusinessView {

    @Override
    public Business login() {
        BusinessDao businessDao = new BusinessDaoImpl();
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入商家编号：");
        Integer businessId = scanner.nextInt();
        System.out.println("请输入登录密码：");
        String password = scanner.next();

        return businessDao.login(businessId,password);
    }

    @Override
    public void showBusiness(Integer businessId) {
        BusinessDao businessDao = new BusinessDaoImpl();
        Business business = businessDao.findBusinessByBusinessId(businessId);
        System.out.println(business);
    }

    @Override
    public void listBusinessAll() {
        BusinessDao businessDao = new BusinessDaoImpl();
        List<Business> allBusiness = businessDao.findAllBusiness();
        System.out.println("商家编号\t商家名称\t商家地址\t商家介绍\t起送费\t配送费");
        for (Business b : allBusiness) {
            System.out.println(b.getBusinessId()+"\t"+b.getBusinessName()+"\t"+b.getBusinessAddress()+"\t"+b.getBusinessExplain()+"\t"+b.getStartPrice()+"\t"+b.getDeliveryPrice()+"\t");
        }
    }

    @Override
    public void listBusiness() {
        BusinessDao businessDao = new BusinessDaoImpl();
        Scanner scanner = new Scanner(System.in);
        String likeName = null;
        String likeAddress = null;

        System.out.println("是否需要输入商家名称关键词(y/n)");
        String need1 = scanner.nextLine();
        if (need1.equals("y")){
            System.out.println("请输入商家名称关键词：");
            likeName = scanner.nextLine();
        }
        System.out.println("是否需要输入商家地址关键词(y/n)");
        String need2 = scanner.nextLine();
        if (need2.equals("y")){
            System.out.println("请输入商家地址关键词：");
            likeAddress = scanner.nextLine();
        }

        List<Business> businesses = businessDao.likeBusiness(likeName,likeAddress);
        System.out.println("商家编号\t商家名称\t商家地址\t商家介绍\t起送费\t配送费");
        for (Business b : businesses) {
            System.out.println(b.getBusinessId()+"\t"+b.getBusinessName()+"\t"+b.getBusinessAddress()+"\t"+b.getBusinessExplain()+"\t"+b.getStartPrice()+"\t"+b.getDeliveryPrice()+"\t");
        }
    }

    @Override
    public void saveBusiness() {
        BusinessDao businessDao = new BusinessDaoImpl();
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要添加的商家名称：");
        String businessName = scanner.nextLine();
        Integer businessId = businessDao.addBusiness(businessName);
        System.out.println("添加成功！该商家编号为："+businessId);
    }

    @Override
    public void deleteBusiness() {
        BusinessDao businessDao = new BusinessDaoImpl();
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要删除的商家编号：");
        Integer businessId = scanner.nextInt();
        businessDao.deleteBusiness(businessId);
        System.out.println("删除商家成功！");
    }

    @Override
    public void editBusiness(Integer businessId) {
        BusinessDao businessDao = new BusinessDaoImpl();
        Business business = businessDao.findBusinessByBusinessId(businessId);
        System.out.println(business);

        Scanner scanner = new Scanner(System.in);
        String inputStr = "";
        System.out.println("是否修改商家名称(y/n)：");
        inputStr = scanner.next();
        if(inputStr.equals("y")) {
            System.out.println("请输入新的商家名称：");
            business.setBusinessName(scanner.next());
        }

        System.out.println("是否修改商家地址(y/n)：");
        inputStr = scanner.next();
        if(inputStr.equals("y")) {
            System.out.println("请输入新的商家地址：");
            business.setBusinessAddress(scanner.next());
        }

        System.out.println("是否修改商家介绍(y/n)：");
        inputStr = scanner.next();
        if(inputStr.equals("y")) {
            System.out.println("请输入新的商家介绍：");
            business.setBusinessExplain(scanner.next());
        }

        System.out.println("是否修改起送费(y/n)：");
        inputStr = scanner.next();
        if(inputStr.equals("y")) {
            System.out.println("请输入新的起送费：");
            business.setStartPrice(scanner.nextDouble());
        }

        System.out.println("是否修改配送费(y/n)：");
        inputStr = scanner.next();
        if(inputStr.equals("y")) {
            System.out.println("请输入新的配送费：");
            business.setDeliveryPrice(scanner.nextDouble());
        }

        int result = businessDao.updateBusiness(business);
        if(result>0) {
            System.out.println("\n修改商家信息成功！\n");
        }else {
            System.out.println("\n修改商家信息失败！\n");
        }
    }

    @Override
    public void updatePassword(Integer businessId) {
        BusinessDao businessDao = new BusinessDaoImpl();
        Business business = businessDao.findBusinessByBusinessId(businessId);

        Scanner scanner = new Scanner(System.in);
        System.out.println("\n请输入旧密码：");
        String oldPass = scanner.next();
        System.out.println("\n请输入新密码：");
        String password = scanner.next();
        System.out.println("\n请再次输入新密码：");
        String beginPassword = scanner.next();

        if(!business.getPassword().equals(oldPass)) {
            System.out.println("\n旧密码输入错误！");
        }else if(!password.equals(beginPassword)) {
            System.out.println("\n两次输入密码不一致！");
        }else {
            int result = businessDao.updatePassword(businessId, password);
            if(result>0) {
                System.out.println("\n修改密码成功！");
            }else {
                System.out.println("\n修改密码失败！");
            }
        }
    }

}
